package logic

import (
	"context"
	"strconv"

	"mall/common/globalkey"
	"mall/common/modelx"
	"mall/service/backend/common/i18n"
	"mall/service/blog/model"
	"mall/service/blog/rpc/internal/svc"
	"mall/service/blog/rpc/types/blog"

	"github.com/zeromicro/go-zero/core/logx"
)

type BatchSyncQiniuLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBatchSyncQiniuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BatchSyncQiniuLogic {
	return &BatchSyncQiniuLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BatchSyncQiniuLogic) BatchSyncQiniu(in *blog.BatchSyncQiniuReq) (*blog.BaseResp, error) {
	logx.Infof("BatchSyncQiniu BatchSyncQiniuReq :%+v", in)
	switch in.SyncType {
	case uint64(globalkey.AttachmentLogTypePost):
		return l.syncQiniuPost(in)
	case uint64(globalkey.AttachmentLogTypeKnowledgeBase):
		return l.syncQiniuBook(in)
	case uint64(globalkey.AttachmentLogTypePage):
		return l.syncQiniuPage(in)
	case uint64(globalkey.AttachmentLogTypeTerm):
		return l.syncQiniuTerm(in)
	}
	return &blog.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}

func (l *BatchSyncQiniuLogic) syncQiniuPost(in *blog.BatchSyncQiniuReq) (*blog.BaseResp, error) {
	//New: "https://static.scentrobe.com/uploads/forum/image/202401/018ccf18-84a5-7eac-8f19-073f32c36663.jpg"
	//Old: "https://www.scentrobe.com/proxy2/static/uploads/forum/image/202401/018ccf17-0d83-7eac-9d02-60deb2c5c525.jpg"
	//Guid: "/uploads/2023/04/c77e5d9196c2286f17dc3c31b196fbd7.png"
	// forum_attachment_relationships
	var aidUrlMap = make(map[uint64][]uint64)
	var ids []any
	for aid := range in.Url {
		ids = append(ids, aid)
	}

	//修改
	attFilter := make(map[string]map[string][]any)
	attFilter[model.BlogAttachmentRelationshipColumns.ObjectType] = map[string][]any{
		"equalTo": {globalkey.AttachmentLogTypeForumPost},
	}
	attFilter[model.BlogAttachmentRelationshipColumns.AttachmentID] = map[string][]any{
		"in": ids,
	}
	rel, _, err := l.svcCtx.KBEntry.GetAttachmentRelationshipList(l.ctx, model.M{}, -1, attFilter)
	if err != nil {
		return nil, err
	}
	if len(rel) > 0 {
		for _, v := range rel {
			aidUrlMap[v.AttachmentID] = append(aidUrlMap[v.AttachmentID], v.AttachmentRelationshipID)
		}
		for aid, v := range aidUrlMap {
			_, err = l.svcCtx.KBEntry.UpdateMediaBatch(l.ctx, nil, v, model.M{
				model.BlogAttachmentRelationshipColumns.AttachmentImageURL: in.Url[aid],
			})
			if err != nil {
				return nil, err
			}
		}
	}

	//修改forum_meta
	//1、查询 meta_key=_thumbnail_id && meta_value=AttachmentID  得到posId
	//2、查询 meta_key=_thumbnail_url && post_id=postID  meta_value重新赋值
	//3、修改
	//1、
	metaFilter := make(map[string]map[string][]any)
	metaFilter[model.BlogPostMetumColumns.MetaKey] = map[string][]any{
		"equalTo": {"_thumbnail_id"},
	}
	metaFilter[model.BlogPostMetumColumns.MetaValue] = map[string][]any{
		"in": ids,
	}
	metaList, _, err := l.svcCtx.Post.GetMeta(l.ctx, nil, -1, metaFilter)
	if err != nil {
		return nil, err
	}
	//2、
	if len(metaList) > 0 {
		for _, v := range metaList {
			metaFilteri := make(map[string]map[string][]any)
			metaFilteri[model.BlogPostMetumColumns.MetaKey] = map[string][]any{
				"equalTo": {"_thumbnail_url"},
			}
			metaFilteri[model.BlogPostMetumColumns.PostID] = map[string][]any{
				"equalTo": {v.PostID},
			}
			metaListi, _, err := l.svcCtx.Post.GetMeta(l.ctx, nil, -1, metaFilteri)
			if err != nil {
				return nil, err
			}
			//3、
			if len(metaListi) != 0 && metaListi[0] != nil {
				meta := metaListi[0]
				aid, err := strconv.ParseUint(modelx.FormatNullDotString(v.MetaValue), 10, 64)
				if err != nil {
					return nil, err
				}
				meta.MetaValue = modelx.GetNullString(in.Url[aid])
				err = l.svcCtx.Post.UpdateMeta(l.ctx, nil, meta)
				if err != nil {
					return nil, err
				}
			}
		}
	}
	return &blog.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}
func (l *BatchSyncQiniuLogic) syncQiniuBook(in *blog.BatchSyncQiniuReq) (*blog.BaseResp, error) {
	//New: "https://static.scentrobe.com/uploads/forum/image/202401/018ccf18-84a5-7eac-8f19-073f32c36663.jpg"
	//Old: "https://www.scentrobe.com/proxy2/static/uploads/forum/image/202401/018ccf17-0d83-7eac-9d02-60deb2c5c525.jpg"
	//Guid: "/uploads/2023/04/c77e5d9196c2286f17dc3c31b196fbd7.png"
	// forum_attachment_relationships
	var aidUrlMap = make(map[uint64][]uint64)
	var ids []any
	for aid := range in.Url {
		ids = append(ids, aid)
	}

	//修改forum_post
	attFilter := make(map[string]map[string][]any)
	attFilter[model.BlogAttachmentRelationshipColumns.ObjectType] = map[string][]any{
		"equalTo": {globalkey.AttachmentLogTypeKnowledgeBase},
	}
	attFilter[model.BlogAttachmentRelationshipColumns.AttachmentID] = map[string][]any{
		"in": ids,
	}
	rel, _, err := l.svcCtx.KBEntry.GetAttachmentRelationshipList(l.ctx, model.M{}, -1, attFilter)
	if err != nil {
		return nil, err
	}
	if len(rel) > 0 {
		for _, v := range rel {
			aidUrlMap[v.AttachmentID] = append(aidUrlMap[v.AttachmentID], v.AttachmentRelationshipID)
		}
		for aid, v := range aidUrlMap {
			_, err = l.svcCtx.KBEntry.UpdateMediaBatch(l.ctx, nil, v, model.M{
				model.BlogAttachmentRelationshipColumns.AttachmentImageURL: in.Url[aid],
			})
			if err != nil {
				return nil, err
			}
		}
	}

	//修改forum_meta
	//1、查询 meta_key=_thumbnail_id && meta_value=AttachmentID  得到posId
	//2、查询 meta_key=_thumbnail_url && post_id=postID  meta_value重新赋值
	//3、修改
	//1、
	metaFilter := make(map[string]map[string][]any)
	metaFilter[model.BlogKnowledgeBaseMetumColumns.MetaKey] = map[string][]any{
		"equalTo": {"_thumbnail_id"},
	}
	metaFilter[model.BlogKnowledgeBaseMetumColumns.MetaValue] = map[string][]any{
		"in": ids,
	}
	metaList, _, err := l.svcCtx.KBEntry.GetMeta(l.ctx, nil, -1, metaFilter)
	if err != nil {
		return nil, err
	}
	//2、
	if len(metaList) > 0 {
		for _, v := range metaList {
			metaFilteri := make(map[string]map[string][]any)
			metaFilteri[model.BlogKnowledgeBaseMetumColumns.MetaKey] = map[string][]any{
				"equalTo": {"_thumbnail_url"},
			}
			metaFilteri[model.BlogKnowledgeBaseMetumColumns.KnowledgeBaseID] = map[string][]any{
				"equalTo": {v.KnowledgeBaseID},
			}
			metaListi, _, err := l.svcCtx.KBEntry.GetMeta(l.ctx, nil, -1, metaFilteri)
			if err != nil {
				return nil, err
			}
			//3、
			if len(metaListi) != 0 && metaListi[0] != nil {
				meta := metaListi[0]
				aid, err := strconv.ParseUint(modelx.FormatNullDotString(v.MetaValue), 10, 64)
				if err != nil {
					return nil, err
				}
				meta.MetaValue = modelx.GetNullString(in.Url[aid])
				_, err = l.svcCtx.KBEntry.UpdateMeta(l.ctx, nil, meta, 0)
				if err != nil {
					return nil, err
				}
			}
		}
	}
	return &blog.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}

func (l *BatchSyncQiniuLogic) syncQiniuPage(in *blog.BatchSyncQiniuReq) (*blog.BaseResp, error) {
	//New: "https://static.scentrobe.com/uploads/forum/image/202401/018ccf18-84a5-7eac-8f19-073f32c36663.jpg"
	//Old: "https://www.scentrobe.com/proxy2/static/uploads/forum/image/202401/018ccf17-0d83-7eac-9d02-60deb2c5c525.jpg"
	//Guid: "/uploads/2023/04/c77e5d9196c2286f17dc3c31b196fbd7.png"
	// forum_attachment_relationships
	var aidUrlMap = make(map[uint64][]uint64)
	var ids []any
	for aid := range in.Url {
		ids = append(ids, aid)
	}

	//修改
	attFilter := make(map[string]map[string][]any)
	attFilter[model.BlogAttachmentRelationshipColumns.ObjectType] = map[string][]any{
		"equalTo": {globalkey.AttachmentLogTypePage},
	}
	attFilter[model.BlogAttachmentRelationshipColumns.AttachmentID] = map[string][]any{
		"in": ids,
	}
	rel, _, err := l.svcCtx.KBEntry.GetAttachmentRelationshipList(l.ctx, model.M{}, -1, attFilter)
	if err != nil {
		return nil, err
	}
	if len(rel) > 0 {
		for _, v := range rel {
			aidUrlMap[v.AttachmentID] = append(aidUrlMap[v.AttachmentID], v.AttachmentRelationshipID)
		}
		for aid, v := range aidUrlMap {
			_, err = l.svcCtx.KBEntry.UpdateMediaBatch(l.ctx, nil, v, model.M{
				model.BlogAttachmentRelationshipColumns.AttachmentImageURL: in.Url[aid],
			})
			if err != nil {
				return nil, err
			}
		}
	}

	//修改forum_meta
	//1、查询 meta_key=_thumbnail_id && meta_value=AttachmentID  得到posId
	//2、查询 meta_key=_thumbnail_url && post_id=postID  meta_value重新赋值
	//3、修改
	//1、
	metaFilter := make(map[string]map[string][]any)
	metaFilter[model.BlogPageMetumTableColumns.MetaKey] = map[string][]any{
		"equalTo": {"_thumbnail_id"},
	}
	metaFilter[model.BlogPageMetumTableColumns.MetaValue] = map[string][]any{
		"in": ids,
	}
	metaList, _, err := l.svcCtx.Page.GetMeta(l.ctx, nil, -1, metaFilter)
	if err != nil {
		return nil, err
	}
	//2、
	if len(metaList) > 0 {
		for _, v := range metaList {
			metaFilteri := make(map[string]map[string][]any)
			metaFilteri[model.BlogPageMetumTableColumns.MetaKey] = map[string][]any{
				"equalTo": {"_thumbnail_url"},
			}
			metaFilteri[model.BlogPageMetumTableColumns.PageID] = map[string][]any{
				"equalTo": {v.PageID},
			}
			metaListi, _, err := l.svcCtx.Page.GetMeta(l.ctx, nil, -1, metaFilteri)
			if err != nil {
				return nil, err
			}
			//3、
			if len(metaListi) != 0 && metaListi[0] != nil {
				meta := metaListi[0]
				aid, err := strconv.ParseUint(modelx.FormatNullDotString(v.MetaValue), 10, 64)
				if err != nil {
					return nil, err
				}
				meta.MetaValue = modelx.GetNullString(in.Url[aid])
				err = l.svcCtx.Page.UpdateMeta(l.ctx, nil, meta)
				if err != nil {
					return nil, err
				}
			}
		}
	}
	return &blog.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}

func (l *BatchSyncQiniuLogic) syncQiniuTerm(in *blog.BatchSyncQiniuReq) (*blog.BaseResp, error) {
	//New: "https://static.scentrobe.com/uploads/forum/image/202401/018ccf18-84a5-7eac-8f19-073f32c36663.jpg"
	//Old: "https://www.scentrobe.com/proxy2/static/uploads/forum/image/202401/018ccf17-0d83-7eac-9d02-60deb2c5c525.jpg"
	//Guid: "/uploads/2023/04/c77e5d9196c2286f17dc3c31b196fbd7.png"
	// forum_attachment_relationships
	var aidUrlMap = make(map[uint64][]uint64)
	var ids []any
	for aid := range in.Url {
		ids = append(ids, aid)
	}

	//修改
	attFilter := make(map[string]map[string][]any)
	attFilter[model.BlogAttachmentRelationshipColumns.ObjectType] = map[string][]any{
		"equalTo": {globalkey.AttachmentLogTypeTerm},
	}
	attFilter[model.BlogAttachmentRelationshipColumns.AttachmentID] = map[string][]any{
		"in": ids,
	}
	rel, _, err := l.svcCtx.KBEntry.GetAttachmentRelationshipList(l.ctx, model.M{}, -1, attFilter)
	if err != nil {
		return nil, err
	}
	if len(rel) > 0 {
		for _, v := range rel {
			aidUrlMap[v.AttachmentID] = append(aidUrlMap[v.AttachmentID], v.AttachmentRelationshipID)
		}
		for aid, v := range aidUrlMap {
			_, err = l.svcCtx.KBEntry.UpdateMediaBatch(l.ctx, nil, v, model.M{
				model.BlogAttachmentRelationshipColumns.AttachmentImageURL: in.Url[aid],
			})
			if err != nil {
				return nil, err
			}
		}
	}

	//修改forum_meta
	//1、查询 meta_key=_thumbnail_id && meta_value=AttachmentID  得到posId
	//2、查询 meta_key=_thumbnail_url && post_id=postID  meta_value重新赋值
	//3、修改
	//1、
	metaFilter := make(map[string]map[string][]any)
	metaFilter[model.BlogTermMetumColumns.MetaKey] = map[string][]any{
		"equalTo": {"_thumbnail_id"},
	}
	metaFilter[model.BlogTermMetumColumns.MetaValue] = map[string][]any{
		"in": ids,
	}
	metaList, _, err := l.svcCtx.Term.GetMeta(l.ctx, nil, -1, metaFilter)
	if err != nil {
		return nil, err
	}
	//2、
	if len(metaList) > 0 {
		for _, v := range metaList {
			metaFilteri := make(map[string]map[string][]any)
			metaFilteri[model.BlogTermMetumColumns.MetaKey] = map[string][]any{
				"equalTo": {"_thumbnail_url"},
			}
			metaFilteri[model.BlogTermMetumColumns.TermID] = map[string][]any{
				"equalTo": {v.TermID},
			}
			metaListi, _, err := l.svcCtx.Term.GetMeta(l.ctx, nil, -1, metaFilteri)
			if err != nil {
				return nil, err
			}
			//3、
			if len(metaListi) != 0 && metaListi[0] != nil {
				meta := metaListi[0]
				aid, err := strconv.ParseUint(modelx.FormatNullDotString(v.MetaValue), 10, 64)
				if err != nil {
					return nil, err
				}
				meta.MetaValue = modelx.GetNullString(in.Url[aid])
				err = l.svcCtx.Term.UpdateMeta(l.ctx, nil, meta)
				if err != nil {
					return nil, err
				}
			}
		}
	}
	return &blog.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}
